package com.qk.exe2;

import java.lang.reflect.Field;

public class MyObjectFactory {
	
	public Object getObject(String className) {
		Object obj = null;
		try {
			Class clazz = Class.forName(className);
			obj = clazz.newInstance();
			Field[] fields = clazz.getDeclaredFields();
			for (Field field: fields) {
				field.setAccessible(true);
				if ("java.lang.String".equals(field.getType().getTypeName())) {
					field.set(obj, field.getName() + " initial value");
				} else if ("int".equals(field.getType().getTypeName())) {
					field.set(obj, 1);
				} else if ("double".equals(field.getType().getTypeName())) {
					field.set(obj, 1.0);
				}
			}
			return obj;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return obj;
	}
	
}
